package kxfang.com.food.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;


import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import kxfang.com.base.fragment.MvvmLazyFragment;
import kxfang.com.base.utils.GsonUtils;
import kxfang.com.base.viewmodel.IMvvmBaseViewModel;
import kxfang.com.common.bean.HomeStoreModel;
import kxfang.com.common.config.HttpConfig;
import kxfang.com.common.parameter.StoreListPackage;
import kxfang.com.common.utils.HawkUtil;
import kxfang.com.common.views.WrapContentHeightViewPager;
import kxfang.com.food.R;
import kxfang.com.food.databinding.FoodFragmentGoodsItemBinding;
import kxfang.com.food.databinding.FoodSjLayoutBinding;
import kxfang.com.http.EasyHttp;
import kxfang.com.http.callback.SimpleCallBack;
import kxfang.com.http.exception.ApiException;

/**
 * Created by ZJ
 * on 2019-12-11
 * Class Content 生活服务下的商家页面
 */
public class SJFragment extends MvvmLazyFragment<FoodSjLayoutBinding, IMvvmBaseViewModel> {
    private BaseQuickAdapter<HomeStoreModel, BaseViewHolder> adapter;
    private double Lat, Lng;
    private int page = 1;
    private List<HomeStoreModel> tempList = new ArrayList<>();

    public SJFragment() {
    }

    @Override
    protected void onFragmentFirstVisible() {
        super.onFragmentFirstVisible();
        getStoreInfo();
        initView();
    }

    @Override
    public int getLayoutId() {
        return R.layout.food_sj_layout;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected IMvvmBaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    private void initView() {
        viewDataBinding.srlFoodStore.setOnLoadMoreListener(refreshLayout -> {
            ++page;
            getStoreInfo();
        });
        viewDataBinding.srlFoodStore.setEnableRefresh(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        viewDataBinding.sjLayoutRcView.setLayoutManager(linearLayoutManager);
        adapter = new BaseQuickAdapter<HomeStoreModel, BaseViewHolder>(R.layout.food_fragment_goods_item,tempList) {
            @Override
            protected void onItemViewHolderCreated(@NotNull BaseViewHolder viewHolder, int viewType) {
                DataBindingUtil.bind(viewHolder.itemView);
            }

            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, @Nullable HomeStoreModel homeStoreModel) {

            }
        };
        viewDataBinding.sjLayoutRcView.setAdapter(adapter);
        adapter.setOnItemClickListener((adapter,v, postion) -> {
//            Intent intent = new Intent(getContext(), HomeStoreDetailFragment.class);
//            intent.putExtra("id", tempList.get(postion).getRUserID());
//            startActivity(intent);
        });
    }

    private void getStoreInfo() {
        StoreListPackage par = new StoreListPackage();
//            par.setCType(1);
        par.setIsRecommands(1);
        par.setCType(1);
        par.setRUserID(HawkUtil.getUserId() + "");
        par.setIndustryValue(1);
        par.setLng(Lng);
        par.setLat(Lat);
        par.setPageSize(10);
        par.setPageIndex(page);
        EasyHttp.post(HttpConfig.getStoreList)
                .cacheKey(HttpConfig.getStoreList)
                .upJson(GsonUtils.toJson(par))
                .execute(new SimpleCallBack<List<HomeStoreModel>>() {
                    @Override
                    public void onError(ApiException e) {
                        
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onSuccess(List<HomeStoreModel> homeStoreModels) {
                        initData(homeStoreModels);
                    }
                });
    }

    private void initData(List<HomeStoreModel> list) {
        if (list != null) {
            tempList.addAll(list);
            adapter.notifyItemInserted(tempList.size());
            if (list.size() < 10) {
                viewDataBinding.srlFoodStore.finishLoadMoreWithNoMoreData();
            } else {
                viewDataBinding.srlFoodStore.finishLoadMore();
            }
        }
    }

    public void setLat(double lat, double lng) {
        Lat = lat;
        Lng = lng;
    }
}
